<template>
    <el-dialog :close-on-click-modal="false" ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible"
        width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="单位名称" prop="unitName" class="w100p">
                <el-input class="w100p" v-model="ruleForm.unitName" clearable placeholder="请输入单位名称"></el-input>
            </el-form-item>
            <el-form-item label="单位符号" prop="unit" class="w100p">
                <el-input class="w100p" v-model="ruleForm.unit" clearable placeholder="请输入单位符号"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import unitApi from '@/api/unit'
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            areaList: [],
            periodTypeList: [
                {
                    label: '一月一结',
                    periodType: 0
                }, {
                    label: '两月一结',
                    periodType: 1
                }, {
                    label: '一季一结',
                    periodType: 2
                }, {
                    label: '半年一结',
                    periodType: 3
                }, {
                    label: '一年一结',
                    periodType: 4
                },
            ],
            rules: {
                unit: [
                    { required: true, message: '请输入单位符号', trigger: 'change' }
                ],

                unitName: [
                    { required: true, message: '请输入单位名称', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData();
            v && this.queryByCondition();
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = unitApi.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = unitApi.edit
                this.addEditName = '编辑'
            }
        },
        async queryByCondition() {
            let rut = await unitApi.queryByCondition({});
            this.areaList = rut.data || []
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>